package com.p2p.controller.loanManagement;

import com.p2p.common.DTO.LoanDTO;
import com.p2p.common.R;
import com.p2p.common.base.RespResult;
import com.p2p.entity.Loan;
import com.p2p.service.loanManagementService.LoanService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/loan")
@Api(value = "借贷信息API",tags = "借贷管理的借贷信息API")
public class LoanController {
    @Autowired
    LoanService loanService;

    /**
     * 添加借贷信息
     * @return RespResult
     */
    @PostMapping("/addLoan")
    @ApiOperation(value = "添加借贷信息",notes = "添加借贷信息",httpMethod = "POST", response = R.class)
    @ApiResponses({
            @ApiResponse(code=200,message="成功"),
            @ApiResponse(code=400,message="代表参数格式不对"),
            @ApiResponse(code=404,message="代表找不到此请求"),
            @ApiResponse(code=500,message="代表后端代码出现异常"),
    })
    @ResponseBody
    public RespResult addLoan(@RequestBody @ApiParam(name = "loan",value = "借贷信息",required = true) Loan loan){

        return loanService.addLoan(loan);
    }

    /**
     * 分页显示所有借贷信息
     * @param currentPage 当前页码数默认第一页
     * @param pageSize 每页显示记录数
     * @return RespResult
     */

    @ApiOperation(value = "分页显示所有借贷信息",notes = "分页显示所有借贷信息",
            httpMethod = "GET", response = R.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage",value = "当前页码数默认第一页",paramType = "query",dataType = "String",required = true),
            @ApiImplicitParam(name = "pageSize",value = "每页显示记录数",paramType = "query",dataType = "String",required = true),
            @ApiImplicitParam(name = "loanDTO",value = "借贷信息",paramType = "query",dataType = "loanDTO",required = true)
    })
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @GetMapping(value = "/findLoanList")
    @ResponseBody
    public RespResult findLoanList
    ( @ApiParam (name = "currentPage",value = "当前页数")Integer currentPage,
      @ApiParam (name = "pageSize",value = "每页显示记录数")Integer pageSize
    ){

        return loanService.selectAllLoanList(currentPage,pageSize);
    }

    /**
     * 修改借贷信息
     * @return RespResult
     */
    @ApiOperation(value = "修改借贷信息",notes = "修改借贷信息",httpMethod = "POST", response = R.class)
    @ApiResponses({
            @ApiResponse(code=200,message="成功"),
            @ApiResponse(code=400,message="代表参数格式不对"),
            @ApiResponse(code=404,message="代表找不到此请求"),
            @ApiResponse(code=500,message="代表后端代码出现异常"),
    })
    @PostMapping("/updateLoan")
    @ResponseBody
    public RespResult updateLoan(@RequestBody @ApiParam(name = "loan",value = "借贷信息",required = true) Loan loan){

        return loanService.updateLoan(loan);
    }

    /**
     * 删除借贷信息
     * @return RespResult
     */
    @ApiOperation(value = "删除借贷用户信息",notes = "删除借贷用户信息",httpMethod = "POST", response = R.class)
    @ApiResponses({
            @ApiResponse(code=200,message="成功"),
            @ApiResponse(code=400,message="代表参数格式不对"),
            @ApiResponse(code=404,message="代表找不到此请求"),
            @ApiResponse(code=500,message="代表后端代码出现异常"),
    })
    @PostMapping("/deleteLoan")
    @ResponseBody
    public RespResult deleteLoan(Long id){
        return loanService.deleteLoan(id);
    }

    /**
     * 根据借贷信息id查询借贷信息
     * @return R
     */
    @ApiOperation(value = "根据借贷信息id查询借贷信息",notes = "根据借贷信息id查询借贷信息",httpMethod = "GET", response = R.class)
    @ApiResponses({
            @ApiResponse(code=200,message="成功"),
            @ApiResponse(code=400,message="代表参数格式不对"),
            @ApiResponse(code=404,message="代表找不到此请求"),
            @ApiResponse(code=500,message="代表后端代码出现异常"),
    })
    @GetMapping("/findLoanById")
    @ResponseBody
    public RespResult findLoanById(Long id){

        return loanService.selectLoanById(id);
    }

    /**
     * 根据条件查询借贷信息
     * @param currentPage 当前页码数默认第一页
     * @param pageSize 每页显示记录数
     * @param loanDTO 借贷信息
     * @return RespResult
     */

    @ApiOperation(value = "根据条件查询借贷信息",notes = "根据条件查询借贷信息",
            httpMethod = "GET", response = R.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage", value = "当前页码数默认第一页", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页显示记录数", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "condition", value = "查询条件", paramType = "query", dataType = "String", required = true)
    })
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @GetMapping(value = "/findLoanByCondition")
    @ResponseBody
    public RespResult findLoanByCondition( @ApiParam(name="currentPage",value ="当前页数")Integer currentPage,
                                           @ApiParam(name="pageSize",value ="每页显示记录数") Integer pageSize,
                                          @RequestBody @ApiParam(name="loanDTO",value ="借贷信息") LoanDTO loanDTO) {
        return loanService.findLoanByCondition(currentPage,pageSize,loanDTO);
    }
    /**
     * 添加图片
     * @param img
     * @param id
     * @param request
     * @return
     */
    @RequestMapping(value = "/addMaterialImage", method = RequestMethod.POST)
    @ApiOperation(value = "添加图片",notes = "添加图片",httpMethod = "POST", response = R.class)
    @ApiResponses({
            @ApiResponse(code=200,message="成功"),
            @ApiResponse(code=400,message="代表参数格式不对"),
            @ApiResponse(code=404,message="代表找不到此请求"),
            @ApiResponse(code=500,message="代表后端代码出现异常"),
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "img", value = "图片", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "id", value = "借贷信息id", paramType = "query", dataType = "String", required = true)
    })
    @ResponseBody
public RespResult addMaterialImage(@RequestParam("img") MultipartFile img, @RequestParam("id") Long id, HttpServletRequest request) {
        return loanService.addMaterialImage(img,id,request);
    }
    /**
     * 添加图片
     * @param img
     * @param id
     * @param request
     * @return
     */
    @RequestMapping(value = "/addLoanInfoImage", method = RequestMethod.POST)
    @ApiOperation(value = "添加图片",notes = "添加图片",httpMethod = "POST", response = R.class)
    @ApiResponses({
            @ApiResponse(code=200,message="成功"),
            @ApiResponse(code=400,message="代表参数格式不对"),
            @ApiResponse(code=404,message="代表找不到此请求"),
            @ApiResponse(code=500,message="代表后端代码出现异常"),
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "img", value = "图片", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "id", value = "借贷信息id", paramType = "query", dataType = "String", required = true)
    })
    @ResponseBody
    public RespResult addLoanInfoImage(@RequestParam("img") MultipartFile img, @RequestParam("id") Long id, HttpServletRequest request) {
        return loanService.addLoanInfoImg(img,id,request);
    }




}
